package org.flexharmony.harmonizer.javadef;

import static org.flexharmony.harmonizer.utils.CommonMethods.*;

import org.flexharmony.harmonizer.QualifiedTypeName;

/**
 * A Java type which is its qualified name and if it is an array or not.
 * 
 * @author Corey Baswell
 */
public class JavaType
{
  public final boolean isArray;
  
  public final QualifiedTypeName qName;
  
  public final QualifiedTypeName superQName;

  public JavaType(String byteCodeType)
  {
    this(byteCodeType, null);
  }

  public JavaType(String byteCodeType, String byteCodeSuperType)
  {
    isArray = byteCodeType.contains("[");
    qName = convertByteCodeTypeToQName(byteCodeType);
    superQName = ((byteCodeSuperType == null) || byteCodeSuperType.equals("java/lang/Object"))  
        ? null : convertByteCodeTypeToQName(byteCodeSuperType);
  }

  /**
   * 
   * @return true if the super class is null or if the super class is in the same
   * package as this type.
   */
  public boolean isSuperClassInSamePackage()
  {
    return ((superQName == null) || qName.packge.equals(superQName.packge));
  }
  
  public String toString()
  {
    String str = qName.toString();
    if (isArray) str += "[]";
    return str;
  }
  
  public boolean equals(Object obj)
  {
    if (obj instanceof JavaType)
    {
      JavaType javaType = (JavaType)obj;
      return ((isArray == javaType.isArray) && qName.equals(javaType.qName));
    }
    else
    {
      return false;
    }
  }
  
  public int hashCode()
  {
    return toString().hashCode();
  }
}
